<?php
require_once ('Controller/Mobile/Common.inc.php');
require_once ('View/Result.inc.php');
require_once ('Model/HouseKeepingMessageModel.inc.php');
require_once ('Model/HouseKeepingPresetMessageModel.inc.php');
require_once ('Model/HouseKeepingPresetMessageLanguageModel.inc.php');
//require_once ('Model/MessageModel.inc.php');
//require_once ('Model/DeviceToMessageModel.inc.php');

final class Controller extends common {
	private $Result;
	private $Loader;
	private $Callback = NULL;
	private $HouseKeepingMessageModel;
	private $HouseKeepingPresetMessageModel;
	private $HouseKeepingPresetMessageLanguageModel;
	//private $DeviceToMessageModel;
	//private $MessageModel;
	public function __construct () {
		parent::__construct();
		$trace = debug_backtrace();
		if (isset ($trace[1])) {
			$this->Loader = $trace[1]['object'];
		}
	}

	public function execude () {
		header ('Content-Type: text/xml');
		$this->Result = new Result ();
		$this->HouseKeepingMessageModel = new HouseKeepingMessageModel ($this->mysql);
		$this->HouseKeepingPresetMessageModel = new HouseKeepingPresetMessageModel ($this->mysql);
		$this->HouseKeepingPresetMessageLanguageModel = new HouseKeepingPresetMessageLanguageModel ($this->mysql);
		//$this->MessageModel = new MessageModel ($this->mysql);
		//$this->DeviceToMessageModel = new DeviceToMessageModel ($this->mysql);
		if (array_key_exists ('action', $_REQUEST)) {
			$this->getFunction ($_REQUEST['action']);
		} else {
			$this->defaultPage ();
		}
		print $this->Result->Create ($this->Callback);
	}

	public function getFunction ($action) {
		switch ($action) {
			case 'getHouseKeepingMessage':
				$this->getHouseKeepingMessagePage ();
				break;
			case 'post':
				$this->postHouseKeepingMessagePage ();
				break;
			case 'reportComplete':
				$this->reportCompleteHouseKeepingMessagePage ();
				break;
			default:
				$this->defaultPage ();
				break;
		}
	}

	public function defaultPage () {
	}

	public function postHouseKeepingMessagePage () {
		$this->CheckDeviceInformation ();
		if ($this->MobileInfo != NULL && $this->MobileInfo->DeviceID != NULL && $this->MobileInfo->Circles != NULL && $this->MobileInfo->Circles[0]->Type == 1) {
			$this->Result->setModulename (sprintf ('M%sF%s', $this->Loader->module, __FUNCTION__));
//			$HouseKeepingPresetMessage
			if (array_key_exists ('text', $_REQUEST) && $_REQUEST['text'] != '' && array_key_exists ('request', $_REQUEST) && ($HouseKeepingPresetMessage = $this->HouseKeepingPresetMessageLanguageModel->GetHouseKeepingLanguageByMessage ($_REQUEST['text'])) != null) {
				$this->Result->setStatus (200);
				$this->Result->setErrorCode (0);
				$this->HouseKeepingMessageModel->AddHouseKeepingMessage ($_REQUEST['text'], $this->MobileInfo->Circles[0]->ID, $_REQUEST['request'], $HouseKeepingPresetMessage[$this->HouseKeepingPresetMessageLanguageModel->messageidRowName]);
			}
		}
	}

	public function getHouseKeepingMessagePage () {
		$this->CheckDeviceInformation ();
		if ($this->MobileInfo != NULL && $this->MobileInfo->DeviceID != NULL && $this->MobileInfo->Circles != NULL && $this->MobileInfo->Circles[0]->Type == 3) {
			$this->Result->setModulename (sprintf ('M%sF%s', $this->Loader->module, __FUNCTION__));
			$this->MessageCount = $this->HouseKeepingMessageModel->CountMessageWithin24Hours ();
			if (array_key_exists ('page', $_REQUEST)) {
				if (ctype_digit ($_REQUEST['page']) && ($_REQUEST['page'] * $this->pagesize) <= $this->MessageCount) {
					$this->page = $_REQUEST['page'];
				} else {
					$this->Result->setErrorCode (1);
					return;
				}
			}
			$this->Result->setStatus (200);
			$this->Result->setErrorCode (0);
			$this->Callback = array ($this, 'getHouseKeepingMessageCallBack');
		}
	}

	private $pagesize = 20;
	private $page = 0;
	public function getHouseKeepingMessageCallBack ($dom, $result) {
		$result->setAttribute ('Itemnum', $this->MessageCount);
		$result->setAttribute ('UnCompletenum', count ($this->HouseKeepingMessageModel->CountUNCompleteWithin24Hours ()));
		$Messageitem = $this->HouseKeepingMessageModel->GetHouseKeepingMessages ($this->page * $this->pagesize, $this->pagesize);
		foreach ($Messageitem as $data) {
//			if ($data[$this->HouseKeepingMessageModel->isCompleteRowName] == 0) {
				$item = $dom->createElement ('item');
				foreach ($data as $key => $value) {
					$item->setAttribute ($key, $value);
				}

				if ($data[$this->HouseKeepingMessageModel->messageidRowName] != 0) {
					$HouseKeepingPresetMessageLanguage = $this->HouseKeepingPresetMessageLanguageModel->GetHouseKeepingLanguageByMessageID($data[$this->HouseKeepingMessageModel->messageidRowName], 'zh-HK');
					$HouseKeepingPresetMessage = $this->HouseKeepingPresetMessageModel->GetHouseKeepingByID ($HouseKeepingPresetMessageLanguage[$this->HouseKeepingPresetMessageLanguageModel->messageidRowName]);
					if ($HouseKeepingPresetMessage[0][$this->HouseKeepingPresetMessageModel->IsCatRowName] == 1) {
						$item->setAttribute ($this->HouseKeepingMessageModel->descriptionRowName, $HouseKeepingPresetMessageLanguage[$this->HouseKeepingPresetMessageLanguageModel->textRowName]);
					} else {
						$HouseKeepingPresetMessage1 = $this->HouseKeepingPresetMessageModel->GetHouseKeepingCatByID ($HouseKeepingPresetMessage[0][$this->HouseKeepingPresetMessageModel->CatidRowName], 'zh-HK');
						$HouseKeepingPresetMessageLanguage1 = $this->HouseKeepingPresetMessageLanguageModel->GetHouseKeepingLanguageByMessageID ($HouseKeepingPresetMessage1[$this->HouseKeepingPresetMessageModel->idRowName], 'zh-HK');
						$item->setAttribute ($this->HouseKeepingMessageModel->descriptionRowName, sprintf ('%s > %s', $HouseKeepingPresetMessageLanguage1[$this->HouseKeepingPresetMessageLanguageModel->textRowName], $HouseKeepingPresetMessageLanguage[$this->HouseKeepingPresetMessageLanguageModel->textRowName]));
					}
				}
//GetHouseKeepingByID
				$item->setAttribute ('CircleName', $this->CircleModel->CheckCircleItemExist ($data[$this->HouseKeepingMessageModel->circleIDRowName])[$this->CircleModel->nameRowName]);
				$Complete_Device_Name = $this->DeviceModel->CheckDeviceItemExistByID ($data[$this->HouseKeepingMessageModel->deviceIDRowName])[$this->DeviceModel->remarkRowName];
				if ($Complete_Device_Name == '') {
					$Complete_Device_Name = $data[$this->HouseKeepingMessageModel->deviceIDRowName];
				}
				$item->setAttribute ('Complete_Device_Name', $Complete_Device_Name);
				$result->appendChild ($item);
//			}
		}
/*		rsort($Messageitem);
		foreach ($Messageitem as $data) {
			if ($data[$this->HouseKeepingMessageModel->isCompleteRowName] == 1) {
				$item = $dom->createElement ('item');
				foreach ($data as $key => $value) {
					$item->setAttribute ($key, $value);
				}
				$item->setAttribute ('CircleName', $this->CircleModel->CheckCircleItemExist ($data[$this->HouseKeepingMessageModel->circleIDRowName])[$this->CircleModel->nameRowName]);
				$result->appendChild ($item);
			}
		}*/

	}

	public function reportCompleteHouseKeepingMessagePage () {
		$this->CheckDeviceInformation ();
		if ($this->MobileInfo != NULL && $this->MobileInfo->DeviceID != NULL && $this->MobileInfo->Circles != NULL && $this->MobileInfo->Circles[0]->Type == 3) {
			$this->Result->setModulename (sprintf ('M%sF%s', $this->Loader->module, __FUNCTION__));
			if (array_key_exists ('id', $_REQUEST) && ctype_digit ($_REQUEST['id'])) {
				$this->Result->setStatus (200);
				$this->Result->setErrorCode (0);
				$this->HouseKeepingMessageModel->SetToComplete ($_REQUEST['id'], $this->MobileInfo->DeviceID);
			}
		}
	}
}
?>
